Reactive Spring by Josh Long
Author:Josh Long
Language: eng
Format: mobi
Tags: Spring, Spring Boot, Reactor, Microservices
Published: 2020-09-13T00:00:00+00:00
When a client sends a message in, we adapt it to a Message object. Message instances store a client ID, the text of the message itself, and a timestamp.
package rsb.ws.chat; import lombok.Data; import lombok.RequiredArgsConstructor; import java.util.Date; @Data @RequiredArgsConstructor class Message { private final String clientId; private final String text; private final Date when; }
The bulk of the chat implementation lives in ChatWebsocketConfiguration.
package rsb.ws.chat; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.reactive.HandlerMapping; import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping; import org.springframework.web.reactive.socket.WebSocketHandler; import org.springframework.web.reactive.socket.WebSocketMessage; import reactor.core.publisher.Flux; import reactor.core.publisher.SignalType; import java.util.Date; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; @Configuration class ChatWebsocketConfiguration { â ChatWebsocketConfiguration(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } â¡ private final Map<String, Connection> sessions = new ConcurrentHashMap<>(); ⢠private final BlockingQueue<Message> messages = new LinkedBlockingQueue<>(); private final ObjectMapper objectMapper; @Bean WebSocketHandler chatWsh() { ⣠var messagesToBroadcast = Flux.<Message>create(sink -> { var submit = Executors.newSingleThreadExecutor().submit(() -> { while (true) { try { sink.next(this.messages.take()); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); sink.onCancel(() -> submit.cancel(true)); }) // .share(); return session -> { ⤠var sessionId = session.getId(); this.sessions.put(sessionId, new Connection(sessionId, session)); var in = session ⥠.receive() // .map(WebSocketMessage::getPayloadAsText) // .map(this::messageFromJson) // .map(msg -> new Message(sessionId, msg.getText(), new Date())) // .map(this.messages::offer)// .doFinally(st -> { ⦠if (st.equals(SignalType.ON_COMPLETE)) {// this.sessions.remove(sessionId);// } }); // var out = messagesToBroadcast â§ .map(this::jsonFromMessage)// .map(session::textMessage); return session.send(out).and(in); }; } ⨠@SneakyThrows private Message messageFromJson(String json) { return this.objectMapper.readValue(json, Message.class); } @SneakyThrows private String jsonFromMessage(Message msg) { return this.objectMapper.writeValueAsString(msg); } @Bean HandlerMapping chatHm() { return new SimpleUrlHandlerMapping() { { this.setUrlMap(Map.of("/ws/chat", chatWsh())); this.setOrder(2); } }; } }
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(25279)
Hello! Python by Anthony Briggs(24330)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(23419)
Kotlin in Action by Dmitry Jemerov(22500)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(21953)
Dependency Injection in .NET by Mark Seemann(21835)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(20697)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(19514)
Grails in Action by Glen Smith Peter Ledbrook(18592)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17028)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(15836)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(13683)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(11845)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11149)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10619)
Hit Refresh by Satya Nadella(9185)
The Kubernetes Operator Framework Book by Michael Dame(8560)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8398)
Robo-Advisor with Python by Aki Ranin(8344)